package com.hnucm.c201901020121.Fragment.Lobby;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.hnucm.c201901020121.Activity.PublishTaskAct;
import com.hnucm.c201901020121.R;
import com.hnucm.c201901020121.Utils.Bmob.BmobTask;

import java.util.ArrayList;
import java.util.List;

import cn.ycbjie.ycstatusbarlib.bar.YCAppBar;


public class LobbyFragment extends Fragment {
    RecyclerView recyclerView;
    List<BmobTask> taskList = new ArrayList<>();
    //MyAdapter myAdapter;
    Button print, bringFood, pickUpThings, bringFruit, pickUpTakeOut, pickUpExpress;
    ViewPager2 viewPager;
    TabLayout tabLayout;
    ImageView publish;
    ArrayList<Fragment> fragmentList=new ArrayList<>();
    String[] tem={"拾物","打印","买饭","水果","外卖","快递"};
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_loddy, container, false);
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        YCAppBar.setStatusBarColor(getActivity(), ContextCompat.getColor(getContext(),R.color.black));
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
     //   MPagerAdapter pagerAdapter=new MPagerAdapter(getChildFragmentManager()) ;
     //   initFragment();
        publish=getActivity().findViewById(R.id.imageView68);
        viewPager = getActivity().findViewById(R.id.viewPager1);
        tabLayout = getActivity().findViewById(R.id.tabLayout1);
        viewPager.setSaveEnabled(false);
        publish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), PublishTaskAct.class);
                startActivity(intent);
            }
        });
        viewPager.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                switch (position) {
                    case 0:
                        return new PickUpThingsFragment();
                    case 1:
                        return new PrintFragment();
                    case 2:
                        return new BringFoodFragment();
                    case 3:
                        return new BringFruitFragment();
                    case 4:
                        return new PickUpTakeOutFragment();
                    default:
                        return new PickUpExpressFragment();
                }
            }

            @Override
            public int getItemCount() {
                return 6;
            }
        });
        viewPager.setCurrentItem(1);
        new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
            switch (position) {
                case 0:
                    tab.setText("拾物");
                    break;
                case 1:
                    tab.setText("打印");
                    break;
                case 2:
                    tab.setText("带饭");
                    break;
                case 3:
                    tab.setText("水果");
                    break;
                case 4:
                    tab.setText("外卖");
                    break;
                default:
                    tab.setText("快递");
            }
        }).attach();
        for(int i=0;i<tabLayout.getTabCount();++i) {
            TabLayout.Tab tab=tabLayout.getTabAt(i);
            if(tab!=null)
            {
                tab.setCustomView(getTabView(i));
            }
        }
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @SuppressLint("ResourceType")
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View view=tab.getCustomView();
                if(view!=null&&view instanceof TextView){
                    ((TextView) view).setTextSize(19);
                    ((TextView) view).setTextColor(Color.parseColor("#FF000000"));
                    ((TextView) view).getPaint();
                }
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view=tab.getCustomView();
                if(view!=null&&view instanceof TextView){
                    ((TextView) view).setTextSize(12);
                    ((TextView) view).setTextColor(Color.parseColor("#CD000000"));
                    ((TextView) view).setTypeface(null, Typeface.NORMAL);
                    ((TextView) view).invalidate();
                }
            }
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                View view=tab.getCustomView();
            }
        });
        tabLayout.getTabAt(0).select();
    }

    private View getTabView(int currentPosition) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_tab, null);
        TextView textView = (TextView) view.findViewById(R.id.tab_item_textview);
        textView.setText(tem[currentPosition]);
        return view;
    }
    private void initFragment() {
        fragmentList.add(new PickUpThingsFragment());
        fragmentList.add(new PrintFragment());
        fragmentList.add(new BringFoodFragment());
        fragmentList.add( new BringFruitFragment());
        fragmentList.add(new PickUpTakeOutFragment());
        fragmentList.add(new PickUpExpressFragment());
    }
}